﻿/*文件注释
 * ===============================================================================
 * 说明：
 * 作者：曾永佑
 * 版本：v1.0
 * 创建日期：2013/12/01
 * 完成日期： 
 * 修改纪录：  
 * ===============================================================================
 */  
                      
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SRF.Common
{
    public static class CollectionExtensions
    {
        /// <summary>
        /// 对应树形结构集合 删除某个节点及其子节点
        /// </summary>
        /// <typeparam name="T">集合元素的对象类型</typeparam>
        /// <param name="collection">对象集合</param>
        /// <param name="id">集合id</param>
        /// <param name="getId">获取对象Id的函数</param>
        /// <param name="getParentId">获取对象父节点Id的函数</param>
        public static void RemoveByTree<T>(this ICollection<T> collection, string id,
            Func<T, string> getId, Func<T, string> getParentId)
        {
            var subList = collection.Where(m => getParentId(m) == id).ToList();
            for (int i = subList.Count() - 1; i >= 0; i--)
            {
                RemoveByTree(collection, getId(subList[i]), getId, getParentId);
                collection.Remove(subList[i]);
            }
            collection.Remove(collection.SingleOrDefault(m => getId(m) == id));
        }
    }
}